package io.hepu.robotize;

import io.hepu.robotize.auth.Jwts;
import io.hepu.robotize.util.Joins;
import io.hepu.robotize.util.Limits;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;

public final class IContext {

    private ConfigurableApplicationContext ctx;
    private JedisPool jedisPool;

    private IContext() {
    }

    public static IContext me() {
        return InstanceHolder.instance;
    }

    private static class InstanceHolder {
        private final static IContext instance = new IContext();
    }

    /**
     * 获取当前Http请求
     *
     * @return HttpServletRequest
     */
    public static HttpServletRequest getRequest() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        assert requestAttributes != null;
        return requestAttributes.getRequest();
    }

    private String getRequestBody(HttpServletRequest request) throws IOException {
        StringBuilder stringBuilder = new StringBuilder();
        String line;
        try (BufferedReader reader = request.getReader()) {
            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line);
            }
        }
        return stringBuilder.toString();
    }

    /**
     * 获取当前Http会话
     *
     * @return HttpSession
     */
    public static HttpSession getSession() {
        return getRequest().getSession();
    }

    /**
     * 获取当前请求头里携带的认证密钥：Token
     *
     * @return Token
     */
    public static String getAuthzHeader() {
        return getRequest().getHeader(IConstant.AUTHORIZATION_HEADER);
    }

    /**
     * 获取当前请求头里携带的认证平台： Platform
     *
     * @return Platform
     */
    public static String getAuthzPlatform() {
        return getRequest().getHeader(IConstant.AUTHORIZATION_PLATFORM);
    }

    /**
     * 获取 JWT ACCESS TOKEN 缓存KEY
     *
     * @param args 影响参数
     * @return cache key
     */
    public static String getJwtCacheKey(Object... args) {
        return IConstant.JWT_ACCESS_TOKEN_PREFIX
                .concat(getAuthzPlatform())
                .concat(":")
                .concat(Joins.concat(args));
    }

    /**
     * 获取 JWT REFRESH TOKEN 缓存KEY
     *
     * @param args 影响参数
     * @return cache key
     */
    public static String getJwtRefreshKey(Object... args) {
        return IConstant.JWT_REFRESH_TOKEN_PREFIX
                .concat(getAuthzPlatform())
                .concat(":")
                .concat(Joins.concat(args));
    }

    /**
     * 获取 SHIRO 缓存KEY
     *
     * @param args 影响参数
     * @return cache key
     */
    public static String getShiroCacheKey(Object... args) {
        return IConstant.SHIRO_CACHE_PREFIX.concat(Joins.concat(args));
    }

    public static String getOperatorId() {
        String authzHeader = IContext.getAuthzHeader();
        if (Limits.nonEmpty(authzHeader)) {
            String token = Jwts.trimScheme(authzHeader);
            return Jwts.getSubject(token);
        }
        return "anonymous";
    }

    /*---------------------
     *  Getter and Setter
     ======================*/
    public ConfigurableApplicationContext getContext() {
        return ctx;
    }

    public void setContext(ConfigurableApplicationContext context) {
        this.ctx = context;
    }

    public JedisPool getJedisPool() {
        return jedisPool;
    }

    public void setJedisPool(JedisPool jedisPool) {
        this.jedisPool = jedisPool;
    }
}
